package com.dabai.qrtools.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import com.dabai.qrtools.R;
import com.dabai.qrtools.other.ImageUtils;
import com.dabai.qrtools.qrcode.BarCodeUtils;
import com.dabai.qrtools.utils.DabaiUtils;
import com.dabai.qrtools.utils.FileUtils;
import com.dabai.qrtools.utils.ThemeUtils;
import com.dabai.qrtools.utils.ViewUtils;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.chip.Chip;
import com.google.android.material.textfield.TextInputLayout;

import java.util.Objects;
import java.util.Random;

public class BarCodeActivity extends AppCompatActivity {

    private Context context;
    private MaterialToolbar mToolbar;
    private TextInputLayout mTil1;
    private ImageView mImg;
    private Chip mChipgroup1chip1;
    private Chip mChipgroup1chip2;
    private TextView mTextview1;


    private String qrContent = "";
    private CardView mImgcard;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar_code);
        initViews();

        context = this;

        ThemeUtils.setAutoUiModeStatusBar(this, this);
        setSupportActionBar(mToolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);


        initListenerEvent();

    }

    private void initListenerEvent() {


        //编辑框变动生成二维码
        mTil1.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {

                qrContent = ViewUtils.getTextInputLayoutText(mTil1);

                if (qrContent.isEmpty()) {
                    return;
                }

                Bitmap qrCodeBitmap = null;
                try {
                    qrCodeBitmap = BarCodeUtils.createBarcode(qrContent, 1000, 300);
                } catch (Exception e) {
                    DabaiUtils.showToast(context, "条形码只能为数字");
              return;
                }
                mTextview1.setText(qrContent);
                mImg.setImageBitmap(qrCodeBitmap);

            }
        });


        mImg.setOnClickListener(v -> {

            if (ViewUtils.getTextInputLayoutText(mTil1).isEmpty()) {
                return;
            }

            try {
                ImageUtils.showBigImage(context,
                        BarCodeUtils.createBarcode(qrContent, 1000, 300));
            } catch (Exception e) {
                DabaiUtils.showToast(context, "条形码只能为数字");
            }
        });


        //保存
        mChipgroup1chip1.setOnClickListener(view -> {

            if (qrContent.isEmpty()) {
                return;
            }

            Bitmap bit;
            try {
                bit = BarCodeUtils.createBarcode(qrContent, 1000, 300);
            } catch (Exception e) {
                DabaiUtils.showToast(context, "条形码只能为数字");
                return;
            }

            boolean isOk = FileUtils.saveBitmapToDCIM(context, bit, "QRTools", "QRTools_barcode_" + new Random().nextInt(100000));
            DabaiUtils.showToast(context, isOk ? "保存成功" : "保存失败");

            if (DabaiUtils.isFunctionFirstOpen(context, "barCodeSave")) {
                DabaiUtils.showDialog(context, "单击保存条形码图，长按保存(条形码+数字)图");
            }

        });
        //保存
        mChipgroup1chip1.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                boolean isOk = FileUtils.saveBitmapToDCIM(context, ViewUtils.getBitmapByView(mImgcard), "QRTools", "QRTools_barcode_" + new Random().nextInt(100000));
                DabaiUtils.showToast(context, isOk ? "保存成功" : "保存失败");

                return true;
            }
        });


        //复制
        mChipgroup1chip2.setOnClickListener(view -> {

            String text = ViewUtils.getTextInputLayoutText(mTil1);

            if (text.isEmpty()) {
                DabaiUtils.showToast(context, "复制失败:内容为空");
                return;
            }

            DabaiUtils.copyText(context, text);

        });

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initViews() {
        mToolbar = findViewById(R.id.toolBar);
        mTil1 = findViewById(R.id.til1);
        mImg = findViewById(R.id.img);
        mChipgroup1chip1 = findViewById(R.id.chipGroup1Chip1);
        mChipgroup1chip2 = findViewById(R.id.chipGroup1Chip2);
        mTextview1 = findViewById(R.id.textView1);
        mImgcard = findViewById(R.id.imgCard);
    }
}